<template>
  <div>
    <!-- 公共头部组件-->
    <Header></Header>
    <div class="m_content">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>

        <el-form-item label="摘要" prop="description">
          <el-input type="textarea" v-model="ruleForm.description"></el-input>
        </el-form-item>

        <!-- mavon-editor -->
        <el-form-item label="内容" prop="content" class="c_form_item">
          <mavon-editor v-model="ruleForm.content" class="c_mavon_editor"></mavon-editor>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
// 引入 其他组件
import Header from "../components/Header";

export default {
  name: "BlogEdit",
  components: { Header },
  data() {
    return {
      ruleForm: {
        id: "",
        title: "",
        description: "",
        content: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入博客标题", trigger: "blur" },
          {
            min: 3,
            max: 300,
            message: "长度在 3 到 300 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入博客摘要", trigger: "blur" },
          {
            min: 3,
            max: 300,
            message: "长度在 3 到 300 个字符",
            trigger: "blur",
          },
        ],
        content: [
          { required: true, message: "请输入博客内容", trigger: "blur" },
          {
            min: 3,
            message: "长度在不小于 3字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 发送请求 保存、更新 博客
          const _this = this;
          this.$axios
            .post("/blog/edit", this.ruleForm, {
              headers: {
                Authorization: localStorage.getItem("token"),
              },
            })
            .then((res) => {
              _this.$alert(res.data.msg, "提示，即将返回列表页面", {
                confirmButtonText: "确定",
                showClose: false,
                callback: (action) => {
                  // 跳转到 列表
                  _this.$router.push("/blogs");
                },
              });
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    // 从请求路径中获取 blogId
    const bId = this.$route.params.blogId;
    // bid有值为 编辑操作
    if (bId) {
      // 根据id 获取 blog信息
      var _this = this;
      _this.$axios.get("/blog/" + bId).then((res) => {
        _this.ruleForm.id = res.data.data.id;
        _this.ruleForm.title = res.data.data.title;
        _this.ruleForm.description = res.data.data.description;
        _this.ruleForm.content = res.data.data.content;
        _this.ruleForm.created = res.data.data.created;
      });
    }
  },
};
</script>
<style scoped>
.m_content {
  margin: 0 auto;
  text-align: center;
}
.v-note-wrapper{
  height: 545px;
}
</style>
